var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        orderList: [],
        order_tab: [
            '全部',
            '待支付',
            '待发货',
            '待收货',
            '待评价',
        ],
        select: 0,
        numList: [],
        isEmpty: false,
        img_url: c.img_url,
        status:0
    },
    onLoad: function (options) {
        utils.checkLogin();
        this.getOrder();

    },
    onShow: function () {
        this.getOrder();
    },

    getOrder: function () {
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var status = this.data.status;
        var data = {
            url: "/v1/ordergoodsno/lists",
            token: token,
            page: 1,
            limit: 10,
            status: status
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                var isEmpty = JSON.stringify(data) == '{}' ? true : false;
                var numList = [];
                data.forEach((item, index) => {
                    var nums = 0;
                    item.forEach((d, i) => {
                        nums += d.nums;
                    })
                    numList[index] = nums;
                })
                that.setData({
                    orderList: data,
                    isEmpty: isEmpty,
                    numList: numList
                });
            } else {

            }
        });
    },
    payOrder: function (e) {
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '确认支付订单？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var order_no = e.currentTarget.dataset.orderno;
                    var data = {
                        url: "/v1/ordergoods/continuePay",
                        token: token,
                        order_no: order_no,
                        openid: wx.getStorageSync("openId"),
                        pay_type: 0,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            //直接支付成功!
                            if (res.data.length == 0) {
                                utils.sucMsg("支付成功");
                                setTimeout(function () {
                                    that.getOrder()
                                }, 1500);
                                return false;
                            }
                            //走在线支付
                            wx.requestPayment({
                                'timeStamp': res.data.timeStamp,
                                'nonceStr': res.data.nonceStr,
                                'package': res.data.package,
                                'signType': 'MD5',
                                'paySign': res.data.paySign,
                                'success': function (res) {
                                    utils.sucMsg("支付成功");
                                    setTimeout(function () {
                                        that.getOrder()
                                    }, 1500);
                                },
                                'fail': function (res) {
                                    utils.errMsg("支付失败");
                                    setTimeout(function () {
                                        that.getOrder()
                                    }, 1500);
                                }
                            })
                        } else {
                            utils.errMsg(res.msg);
                        }
                    });
                } else if (res.cancel) {

                }
            }
        })


    },
    Confirmed: function (e) {
        var order_no = e.currentTarget.dataset.order_no;
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '是否确认收货？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/v1/ordergoods/finish",
                        token: token,
                        order_no: order_no,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            utils.sucMsg("收货成功");
                            setTimeout(function () {
                                that.getOrder();
                            }, 1500);
                        } else {

                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    },
    delOrder:function(e){
        var order_no = e.currentTarget.dataset.orderno;
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '是否确认删除订单？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/v1/ordergoods/delete",
                        token: token,
                        order_no: order_no,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            utils.sucMsg("删除成功");
                            setTimeout(function () {
                                that.getOrder();
                            }, 1500);
                        } else {
                            utils.errMsg(res.msg);
                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    },
    evaluation: function (e) {
        var index = e.currentTarget.dataset.index;
        var data = this.data.orderList;
        var arr = { "pic": data[index][0].pic, "id": data[index][0].id, "goods_id": data[index][0].goods_id, goods_price_id: data[index][0].goods_price_id };
        arr = JSON.stringify(arr);
        wx.navigateTo({
            url: '/pages/shop/evaluation/index?orderExt=' + arr,
        })
    },
    selectTab: function (e) {
        var index = e.currentTarget.dataset.index;

        this.setData({
            select: index,
            status:index
        })
        this.getOrder();
    }
})